package com.api.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.api.common.reqres.Request;
import com.api.common.reqres.Response;
import com.api.component.RequestComponent;
import com.api.component.ResponseComponent;
import com.api.mapper.BackstageUserMapper;
import com.api.mapper.MenuMapper;
import com.api.mapper.RoleMapper;
import com.api.mapper.RoleMenuMapper;
import com.api.model.entity.Menu;
import com.api.model.entity.RoleMenu;
import com.api.model.request.BackstageUserLoginRequest;
import com.api.model.request.RoleMenuListRequest;
import com.api.model.response.RoleMenuListResponse;
import com.api.service.RoleMenuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cfp
 * @since 2019-10-22
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {
	@Autowired
	private RequestComponent requestComponent;
	@Autowired
	private ResponseComponent responseComponent;
	@Autowired
	private RoleMenuMapper roleMenuMapper;
	@Autowired
	private MenuMapper menuMapper;

	@Override
	public Response<RoleMenuListResponse> roleMenuList(Request<RoleMenuListRequest> request) {
		// TODO Auto-generated method stub
		RoleMenuListRequest roleMenuListRequest = request.getBody();
		requestComponent.checkBody(roleMenuListRequest, RoleMenuListRequest.class);
		LambdaQueryWrapper<RoleMenu> roleMenuQueryWrapper = new LambdaQueryWrapper<RoleMenu>();
		roleMenuQueryWrapper.eq(RoleMenu::getRoleid, roleMenuListRequest.getRoleid()).eq(RoleMenu::getStatus, true);
		List<RoleMenu> roleMenuList = roleMenuMapper.selectList(roleMenuQueryWrapper);
		// 获取菜单
		List<Menu> menuList = new ArrayList<Menu>();
		for (RoleMenu roleMenu : roleMenuList) {
			LambdaQueryWrapper<Menu> menuQueryWrapper = new LambdaQueryWrapper<Menu>();
			menuQueryWrapper.eq(Menu::getOid, roleMenu.getMenuid()).eq(Menu::getStatus, true);
			Menu menu = menuMapper.selectOne(menuQueryWrapper);
			menuList.add(menu);
		}
		return responseComponent.toSuccess(menuList);
	}

}
